#!/usr/bin/env python

"""
    Extracts labeled information from log files.

    Before use, edit the file config.py!!

"""

import sys, re, os, copy
from padnums import *
from config import *

try:
    path = sys.argv[1]
    output = sys.argv[2]
except IndexError:
    print "ERROR: Not enough arguments!"
    sys.exit()


to_file = 0
if re.match("(.*)\.csv", output):
    to_file = 1
    out_file_name = os.path.join(path, output)
    out_file = open(out_file_name, 'w')


print files_reg

default = dict([c for c in fields])

data = {}

file_list = os.listdir(path)

sys.stderr.write("Processing...\n<|")

for file_name in file_list:
    file = os.path.join(path, file_name)
    
    if not re.match(files_reg, file):
        continue
    
    if os.path.isdir(file):
        continue
    
    sys.stderr.write("|")

    body = open(file, 'r')

    # This is particular for cluster logs that end with ".oJOB_NUMBER"
    run_id = (re.split("\.o", file_name))[1];

    data[run_id] = copy.deepcopy(default)

    for line in body:
        if re.match("@@(.*)", line):
            aux = re.split("@@|\s", line)
            if aux[1] in default:
                data[run_id][aux[1]] = aux[2]
            else:
                sys.stderr.write("\nWarning, field "+aux[1]+" not defined! @ "+file_name+"\n")
    sys.stderr.write("|")

sys.stderr.write("|||>\n Done!\n")
sys.stderr.write("Writing to file...")

labels = [x for (x,y) in fields]
table = []
table.append(["FileID"])
if to_file: out_file.write("FileID,")

for l in labels:
    table[0].append(l)
    if to_file: out_file.write(l+",")
if to_file: out_file.write("\n")

i=1
for k in data.keys():
    table.append([k])
    if to_file: out_file.write(k+",")
    for l in labels:
        table[i].append(data[k][l])
        if to_file: out_file.write(str(data[k][l])+",")
    i += 1
    if to_file: out_file.write("\n")

sys.stderr.write(" Done!\n")


#Here we use padnum.py by Ryan Ginstrom [http://ginstrom.com/code/]
#pprint_table(sys.stdout, table)
